﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Products;
public partial class shoping_viewcartDefault : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (!Page.IsPostBack)
        {
            if (Session["Shopcart"] != null)
            {
                String cart =Session["Shopcart"].ToString();
                SelectedList mySelectedItems = SelectedList.GetSelectedItems(cart);
                ShowCart(mySelectedItems);
            }
            else
            {
                 Response.Redirect("http://localhost:2011/didong/shoping/Emptycart.html");
            }
           
        }
        //Response.Write(Session["Shopcart"]);
    }
    protected void products_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {

    }
    protected void ShowCart(SelectedList mySelectedItems)
    {
        if (mySelectedItems.Count > 0)
        {
            String cart = "Shopcart";
            this.products.DataSource = mySelectedItems.Items;
            this.products.DataBind();
            cart = SelectedList.UpdateShoppingcart(mySelectedItems);
            Session["Shopcart"] = cart;
            Literal fQuantity = (Literal)(this.products.FindControl("fQuantity"));
            Literal fTotal = (Literal)(this.products.FindControl("fTotal"));
            Literal litHeader = (Literal)(this.products.FindControl("litHeader"));
            Literal litBottom = (Literal)(this.products.FindControl("litBottom"));
            if (fTotal != null && fQuantity != null)
            {
                fQuantity.Text = String.Format("{0:0,00}", mySelectedItems.Quantity);
                fTotal.Text = String.Format("{0:0,00}", mySelectedItems.Total);
                Session["count"] = mySelectedItems.Quantity.ToString();
            }
            if (litHeader != null && litBottom != null)
            {
                //SelectedList.BuildTitle(litHeader, litBottom,"Vn");
            }
        }
        else
        {
           // Response.Redirect("http://localhost:2011/didong/Shoping/emptycart.vienthongsaigon");
        }
    }
    protected void Emptycart_Click(object sender, EventArgs e)
    {
        Session["Shopcart"] = null;
        Response.Redirect("http://localhost:2011/didong/shoping/Emptycart.html");
    }
    protected void Updatecart_Click(object sender, EventArgs e)
    {
     
                SelectedList SelectedItems = new SelectedList();
                String[] cartinfo;
                SelectedProduct mySelectedProduct = new SelectedProduct();
                foreach (ListViewItem myItem in this.products.Items)
                {

                    if (myItem.ItemType == ListViewItemType.DataItem)
                    {
                        HiddenField hdinfo = (HiddenField)myItem.FindControl("hdinfo");
                        TextBox inputquantity = (TextBox)myItem.FindControl("inputquantity");
                        if (hdinfo != null && inputquantity != null)
                        {
                           // Response.Redirect("http://localhost:2011/didong/shoping/Emptycart.html");
                            cartinfo = hdinfo.Value.ToString().Split(',');
                            mySelectedProduct = new SelectedProduct(Convert.ToInt32(cartinfo[0]), cartinfo[1], cartinfo[3], Convert.ToInt32(inputquantity.Text), Convert.ToDecimal(cartinfo[2]));
                            SelectedItems.Add(mySelectedProduct);
                        }
                    }
                }
                ShowCart(SelectedItems);
              
               //Response.Redirect("http://localhost:2011/didong/shoping/Viewcart.aspx");
    }
  
}
